import java.util.Arrays;

public class ArrayDemo02 {

    public static void main(String[] args) {

        // 1.创建两个数组
        int[] array = {4, 2, 8, 7, 6, 9, 1, 0, 10};

        System.out.println("排序前:" + Arrays.toString(array));
        // 从小到大排序(冒泡,相邻两个元素作对比然后交换)
        order1(array);
        System.out.println("排序后(从小到大):" + Arrays.toString(array));

        order2(array);
        System.out.println("排序后(从大到小):" + Arrays.toString(array));

//        int a = 10;
//        int b = 20;
//        System.out.println("a = " + a);
//        System.out.println("b = " + b);
//        System.out.println("=================");
//        int temp = 0; // 临时保存下
//        temp = a;
//        a = b;
//        b = temp;
//        System.out.println("a = " + a);
//        System.out.println("b = " + b);
    }

    private static void order2(int[] array) {
        for (int i = 0; i < array.length; i++) { // 控制比较几轮
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] < array[j + 1]) {
                    // 左边的右表的大则交换
                    int temp = 0;
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    private static void order1(int[] array) {
        for (int i = 0; i < array.length; i++) { // 控制比较几轮
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    // 左边的右表的大则交换
                    int temp = 0;
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}
